package com.renfei.minevideoplayer.opengl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.util.Log;

import com.renfei.minevideoplayer.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * 渲染图片
 */
public class TextureRender implements GLSurfaceView.Renderer {

    private static final String TAG = "TextureRender";
    private Context context;
    private float[] vertexData = {
            -0.9f, -0.9f,
            0.9f, -0.9f,
            -0.9f, 0.9f,
            0.9f, 0.9f
    };

    //屏幕坐标
    private float[] textureData = {
            0f, 1f,
            1f, 1f,
            0f, 0f,
            1f, 0f
    };
    //纹理坐标

  //      private float[] textureData={
  //              0f,0f,
  //              1f,0f,
  //              0f,1f,
  //              1f,1f
  //      };



    private FloatBuffer vertexBuffer;

    private FloatBuffer textureBuffer;

    private int program;
    private int av_Position;//attribute  vec4
    private int af_Position ;  //attribute   vec2
//    private int sTexture;  //uniform sampler2D

    private int bitmapTextureId;


    public TextureRender(Context context) {
        this.context = context;
        vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertexBuffer.put(vertexData).position(0);

        textureBuffer = ByteBuffer.allocateDirect(textureData.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        textureBuffer.put(textureData).position(0);
    }


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        program = OpenGLUtil.createProgram(
                OpenGLUtil.loadRawShaderSource(context, R.raw.vertex_texture_shader),
                OpenGLUtil.loadRawShaderSource(context, R.raw.fragment_texture_shader)
        );
        if (program > 0) {
            av_Position = GLES20.glGetAttribLocation(program, "av_Position");
            af_Position = GLES20.glGetAttribLocation(program, "af_Position");
//            sTexture = GLES20.glGetUniformLocation(program, "sTexture");
            createBitmapTextureId();
        }
    }


    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glClearColor(.01f, 0.4f, 0.7f, 1.0f);

        GLES20.glUseProgram(program);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,bitmapTextureId);

        GLES20.glEnableVertexAttribArray(av_Position);
        GLES20.glVertexAttribPointer(av_Position, 2, GLES20.GL_FLOAT,
                false,0,vertexBuffer);

        GLES20.glEnableVertexAttribArray(af_Position);
        GLES20.glVertexAttribPointer(af_Position,2,GLES20.GL_FLOAT,
                false,0,textureBuffer);


        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
    }


    private void createBitmapTextureId() {
        int[] textures = new int[1];
        GLES20.glGenTextures(1, textures, 0);
        if (textures[0] == 0) {
            Log.e(TAG, "glGenTextures: create texture on error");
            return;
        }
        bitmapTextureId = textures[0];

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, bitmapTextureId);

        //环绕（超出纹理坐标范围）：（s==x t==y GL_REPEAT 重复）
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
        //过滤（纹理像素映射到坐标点）：（缩小、放大：GL_LINEAR线性）
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.og);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,bitmap,0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

        bitmap.recycle();
        bitmap=null;
    }
}
